<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="内容中文标题" prop="contentTitle">
      <el-input v-model="dataForm.contentTitle" placeholder="内容中文标题"></el-input>
    </el-form-item>
    <el-form-item label="内容英文标题" prop="contentTitleEn">
      <el-input v-model="dataForm.contentTitleEn" placeholder="内容英文标题"></el-input>
    </el-form-item>
    <el-form-item label="内容logo" prop="contentLogo">
      <el-input v-model="dataForm.contentLogo" placeholder="内容logo"></el-input>
    </el-form-item>
    <el-form-item label="内容副标题" prop="contentSubtitle">
      <el-input v-model="dataForm.contentSubtitle" placeholder="内容副标题"></el-input>
    </el-form-item>
    <el-form-item label="内容信息" prop="contentMessage">
      <el-input v-model="dataForm.contentMessage" placeholder="内容信息"></el-input>
    </el-form-item>
    <el-form-item label="内容类型" prop="contentType">
      <el-input v-model="dataForm.contentType" placeholder="内容类型"></el-input>
    </el-form-item>
    <el-form-item label="排序码" prop="contentSort">
      <el-input v-model="dataForm.contentSort" placeholder="排序码"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 0：存在 1：已删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除 0：存在 1：已删除"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：可用 1：不可用" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态  0：可用 1：不可用"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          contentId: 0,
          contentTitle: '',
          contentTitleEn: '',
          contentLogo: '',
          contentSubtitle: '',
          contentMessage: '',
          contentType: '',
          contentSort: '',
          delFlag: '',
          status: '',
          createTime: '',
          updateTime: '',
          createUser: ''
        },
        dataRule: {
          contentTitle: [
            { required: true, message: '内容中文标题不能为空', trigger: 'blur' }
          ],
          contentTitleEn: [
            { required: true, message: '内容英文标题不能为空', trigger: 'blur' }
          ],
          contentLogo: [
            { required: true, message: '内容logo不能为空', trigger: 'blur' }
          ],
          contentSubtitle: [
            { required: true, message: '内容副标题不能为空', trigger: 'blur' }
          ],
          contentMessage: [
            { required: true, message: '内容信息不能为空', trigger: 'blur' }
          ],
          contentType: [
            { required: true, message: '内容类型不能为空', trigger: 'blur' }
          ],
          contentSort: [
            { required: true, message: '排序码不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除 0：存在 1：已删除不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  0：可用 1：不可用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.contentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.contentId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webcontent/info/${this.dataForm.contentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contentTitle = data.webContent.contentTitle
                this.dataForm.contentTitleEn = data.webContent.contentTitleEn
                this.dataForm.contentLogo = data.webContent.contentLogo
                this.dataForm.contentSubtitle = data.webContent.contentSubtitle
                this.dataForm.contentMessage = data.webContent.contentMessage
                this.dataForm.contentType = data.webContent.contentType
                this.dataForm.contentSort = data.webContent.contentSort
                this.dataForm.delFlag = data.webContent.delFlag
                this.dataForm.status = data.webContent.status
                this.dataForm.createTime = data.webContent.createTime
                this.dataForm.updateTime = data.webContent.updateTime
                this.dataForm.createUser = data.webContent.createUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webcontent/${!this.dataForm.contentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'contentId': this.dataForm.contentId || undefined,
                'contentTitle': this.dataForm.contentTitle,
                'contentTitleEn': this.dataForm.contentTitleEn,
                'contentLogo': this.dataForm.contentLogo,
                'contentSubtitle': this.dataForm.contentSubtitle,
                'contentMessage': this.dataForm.contentMessage,
                'contentType': this.dataForm.contentType,
                'contentSort': this.dataForm.contentSort,
                'delFlag': this.dataForm.delFlag,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
